/*
 * Encoder class for privateMessage
 */
package ws.websk.chatEndpoint2;

import java.io.StringWriter;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonWriter;
import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;
import ws.websk.chatEndpoint2.messages.PrivateMessage;

/**
 *
 * @author Jens-Marius Nergård Hansen
 */
public class privateMsgEncoder implements Encoder.Text<PrivateMessage> {

    @Override
    public String encode(PrivateMessage object) throws EncodeException {
        
        JsonObject jsonObject = 
                Json.createObjectBuilder()
                .add("type", object.getType())
                .add("message", object.getMessage())
                .add("receiver", object.getReceiver())
                .add("sender", object.getSender())
                .build();

        StringWriter stWriter = new StringWriter();
        JsonWriter jsonWriter = Json.createWriter(stWriter);
        jsonWriter.writeObject(jsonObject);
        jsonWriter.close();

        String jsonData = stWriter.toString();
        return jsonData;
    }

    @Override
    public void init(EndpointConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void destroy() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}